package com.example.raising_skills.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.view_count.entity.ViewCountDO;
import com.example.view_count.service.ViewCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ThreadService {
    @Autowired
    private ViewCountService viewCountService;
    @Async("taskExecutor")
    public void updateViewCount() {

        //查询次数
        int viewCounts = viewCountService.queryViewCount();
//        System.out.println(viewCounts);
        ViewCountDO viewCountDO = new ViewCountDO();
        viewCountDO.setViewcount(viewCounts + 1 );
        LambdaUpdateWrapper<ViewCountDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ViewCountDO::getId,1);
        //
        updateWrapper.eq(ViewCountDO::getViewcount,viewCounts);
        //
        viewCountService.getBaseMapper().update(viewCountDO,updateWrapper);
        //更新数据库数据

        try {
            Thread.sleep(4000);
            System.out.println("更新完成了....");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
